﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LevenBlog.Core.Data;
using LevenBlog.Core.Entities;

namespace LevenBlog.Core.Service
{
    public static class Track
    {
        public static int Count(IDbProvider provider)
        {
            return ProviderManager.Factory.Track.Count(provider);
        }

        public static int Count(IDbProvider provider, int blogid)
        {
            return ProviderManager.Factory.Track.CountFromBlogID(provider, blogid);
        }

        public static lb_track Get(IDbProvider provider, int id)
        {
            return ProviderManager.Factory.Track.Get(provider, id);
        }

        public static IList<lb_track> Get(IDbProvider provider, int pageSize, int currentPage)
        {
            return ProviderManager.Factory.Track.Get(provider, pageSize, currentPage);
        }

        public static IList<lb_track> GetFromBlogID(IDbProvider provider, int blogid)
        {
            return ProviderManager.Factory.Track.GetFromBlogID(provider, blogid);
        }

        public static IList<lb_track> GetFromBlogID(IDbProvider provider, int blogid, int pageSize, int currentPage)
        {
            return ProviderManager.Factory.Track.GetFromBlogID(provider, blogid, pageSize, currentPage);
        }

        public static void Insert(IDbProvider provider, lb_track track)
        {
            provider.BeginTransaction();
            try
            {
                lb_state state = State.Get(provider);
                state.tracknum++;
                State.Update(provider, state);
                lb_blog blog = ProviderManager.Factory.Blog.Get(provider, track.blogid);
                blog.tracknum++;
                ProviderManager.Factory.Blog.Update(provider, blog);
                ProviderManager.Factory.Track.Insert(provider, track);
                provider.Commit();
            }
            catch (Exception e)
            {
                provider.Rollback();
                throw new Exception("添加新track信息失败.", e);
            }
        }

        public static void Update(IDbProvider provider, lb_track track)
        {
            ProviderManager.Factory.Track.Update(provider, track);
        }

        public static void Delete(IDbProvider provider, int id)
        {
            provider.BeginTransaction();
            try
            {
                lb_state state = State.Get(provider);
                state.tracknum--;
                State.Update(provider, state);
                lb_track track = ProviderManager.Factory.Track.Get(provider, id);
                lb_blog blog = ProviderManager.Factory.Blog.Get(provider, track.blogid);
                blog.tracknum--;
                ProviderManager.Factory.Blog.Update(provider, blog);
                ProviderManager.Factory.Track.Delete(provider, id);
                provider.Commit();
            }
            catch (Exception e)
            {
                provider.Rollback();
                throw new Exception("删除指定的track失败.", e);
            }
        }
    }
}
